# Copyright 2020 The Pigweed Authors
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.

load("@com_google_protobuf//bazel:java_lite_proto_library.bzl", "java_lite_proto_library")
load("@com_google_protobuf//bazel:proto_library.bzl", "proto_library")
load("@rules_cc//cc:cc_library.bzl", "cc_library")
load("@rules_python//python:proto.bzl", "py_proto_library")
load("@rules_python//sphinxdocs:sphinx_docs_library.bzl", "sphinx_docs_library")
load("//pw_build:compatibility.bzl", "host_backend_alias", "incompatible_with_mcu")
load("//pw_build:copy_to_bin.bzl", "copy_to_bin")
load("//pw_build:pw_facade.bzl", "pw_facade")
load(
    "//pw_protobuf_compiler:pw_proto_library.bzl",
    "pwpb_proto_library",
    "raw_rpc_proto_library",
)
load("//pw_unit_test:pw_cc_test.bzl", "pw_cc_test")

package(
    default_visibility = ["//visibility:public"],
)

licenses(["notice"])

# TODO: pwbug.dev/328679085 - Remove this alias once no-one uses it.
alias(
    name = "facade",
    actual = "pw_log.facade",
)

pw_facade(
    name = "pw_log",
    hdrs = [
        "public/pw_log/config.h",
        "public/pw_log/levels.h",
        "public/pw_log/log.h",
        "public/pw_log/options.h",
        "public/pw_log/short.h",
        "public/pw_log/shorter.h",
    ],
    backend = ":backend",
    strip_include_prefix = "public",
    deps = [
        ":config_override",
        "//pw_preprocessor",
    ],
)

cc_library(
    name = "rate_limited",
    srcs = [
        "rate_limited.cc",
    ],
    hdrs = [
        "public/pw_log/rate_limited.h",
    ],
    features = ["-conversion_warnings"],
    strip_include_prefix = "public",
    deps = [
        ":config_override",
        ":pw_log",
        "//pw_chrono:system_clock",
    ],
)

cc_library(
    name = "args",
    hdrs = [
        "public/pw_log/tokenized_args.h",
    ],
    strip_include_prefix = "public",
    deps = ["//pw_tokenizer"],
)

label_flag(
    name = "config_override",
    build_setting_default = "//pw_build:default_module_config",
)

label_flag(
    name = "backend",
    build_setting_default = ":unspecified_backend",
    # For internal tooling: go/build-cleaner/troubleshooting-faq#keep-dep
    tags = ["keep_dep"],
)

host_backend_alias(
    name = "unspecified_backend",
    backend = "//pw_log_basic",
)

label_flag(
    name = "backend_impl",
    build_setting_default = ":unspecified_backend_impl",
    # For internal tooling: go/build-cleaner/troubleshooting-faq#keep-dep
    tags = ["keep_dep"],
)

host_backend_alias(
    name = "unspecified_backend_impl",
    backend = "//pw_log_basic:impl",
)

cc_library(
    name = "glog_adapter",
    hdrs = [
        "public/pw_log/glog_adapter.h",
        "public/pw_log/glog_adapter_config.h",
        "public/pw_log/internal/glog_adapter.h",
    ],
    strip_include_prefix = "public",
    deps = [
        ":glog_adapter_config_override",
        "//pw_assert:check",
        "//pw_log",
        "//pw_preprocessor",
        "//pw_string",
    ],
)

label_flag(
    name = "glog_adapter_config_override",
    build_setting_default = "//pw_build:default_module_config",
)

cc_library(
    name = "proto_utils",
    srcs = [
        "proto_utils.cc",
    ],
    hdrs = [
        "public/pw_log/proto_utils.h",
    ],
    features = ["-conversion_warnings"],
    strip_include_prefix = "public",
    deps = [
        ":log_proto_pwpb",
        ":pw_log.facade",
        "//pw_bytes",
        "//pw_log_tokenized:headers",
        "//pw_protobuf",
        "//pw_result",
        "//pw_span",
        "//pw_status",
    ],
)

proto_library(
    name = "log_proto",
    srcs = [
        "log.proto",
    ],
    import_prefix = "pw_log/proto",
    strip_import_prefix = "/pw_log",
    deps = [
        "//pw_protobuf:common_proto",
        "//pw_tokenizer:tokenizer_proto",
    ],
)

py_proto_library(
    name = "log_proto_py_pb2",
    deps = [":log_proto"],
)

java_lite_proto_library(
    name = "log_java_proto_lite",
    deps = [":log_proto"],
)

pwpb_proto_library(
    name = "log_proto_pwpb",
    deps = [":log_proto"],
)

raw_rpc_proto_library(
    name = "log_proto_raw_rpc",
    deps = [":log_proto"],
)

# https://pwbug.dev/375207472 - Remove this alias after downstream migration.
alias(
    name = "log_proto_cc.pwpb",
    actual = ":log_proto_pwpb",
)

# https://pwbug.dev/375207472 - Remove this alias after downstream migration.
alias(
    name = "log_proto_cc.raw_rpc",
    actual = ":log_proto_raw_rpc",
)

pw_cc_test(
    name = "test",
    srcs = [
        "basic_log_test.cc",
        "basic_log_test_plain_c.c",
    ],
    features = ["-conversion_warnings"],
    deps = [
        ":pw_log",
        ":pw_log.facade",
        ":rate_limited",
        "//pw_preprocessor",
    ],
)

pw_cc_test(
    name = "glog_adapter_test",
    srcs = [
        "glog_adapter_test.cc",
    ],
    deps = [":glog_adapter"],
)

pw_cc_test(
    name = "proto_utils_test",
    srcs = [
        "proto_utils_test.cc",
    ],
    features = ["-conversion_warnings"],
    deps = [
        ":log_proto_pwpb",
        ":proto_utils",
        ":pw_log.facade",
        "//pw_bytes",
        "//pw_containers:algorithm",
        "//pw_preprocessor",
        "//pw_protobuf",
        "//pw_protobuf:bytes_utils",
    ],
)

pw_cc_test(
    name = "log_enum_test",
    srcs = [
        "log_enum_test.cc",
    ],
    deps = [
        ":args",
        "//pw_log",
        "//pw_tokenizer",
        "//pw_unit_test:constexpr",
    ],
)

filegroup(
    name = "doxygen",
    srcs = [
        "public/pw_log/config.h",
        "public/pw_log/glog_adapter.h",
        "public/pw_log/glog_adapter_config.h",
        "public/pw_log/levels.h",
        "public/pw_log/log.h",
        "public/pw_log/options.h",
        "public/pw_log/proto_utils.h",
        "public/pw_log/rate_limited.h",
        "public/pw_log/short.h",
        "public/pw_log/shorter.h",
        "public/pw_log/tokenized_args.h",
    ],
)

sphinx_docs_library(
    name = "docs",
    srcs = [
        "backends.rst",
        "docs.rst",
        "log.proto",
        "protobuf.rst",
        "tokenized_args.rst",
    ],
    prefix = "pw_log/",
    target_compatible_with = incompatible_with_mcu(),
)

copy_to_bin(
    name = "js_protos",
    srcs = [
        "log.proto",
    ],
)
